/*
 * All objects that are drawn and have a 
 * position should extends this class
 */
package gameElements;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class Sprite {
	private int xPosition;
	private int yPosition;
	private int width;
	private int height;
	private Rectangle bounding;
	private BufferedImage spriteSheet;
	private ArrayList<BufferedImage> imageList;
	private int currentImageIndex = 0;
	
	
	public Sprite(){
		
		xPosition = 0;
		yPosition = 0;
		width = 10;
		height = 10;
		bounding = new Rectangle(width, height);
		imageList = new ArrayList<BufferedImage>();
	}
	
	public void loadImages(String fileName, int numHorSprites, int numVertSprites){
		
		File imageFile = new File(fileName);
		try{
			spriteSheet = ImageIO.read(imageFile);
		}catch(IOException e){
			e.printStackTrace();
		}
		
		//divide spriteSheet using numHorSprites and numVertSprites 
		//and store into imageList
		for (int i = 0; i < numHorSprites; i++) {
			for (int j = 0; j < numVertSprites; j++) {
				imageList.add(spriteSheet.getSubimage(i * numHorSprites, j * numVertSprites, spriteSheet.getWidth() / numHorSprites, spriteSheet.getHeight() / numVertSprites));
				
			}
		}
		
				
	}
	
	public void update(){

		bounding = new Rectangle(xPosition, yPosition, xPosition + width, yPosition + height);
		
		
	}
	
	public void setPosition(int x, int y){
		xPosition = x;
		yPosition = y;
	}
	
	public int getXPosition(){
		return xPosition;
	}
	
	public int getYPosition(){
		return yPosition;
	}
	
	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getCurrentImageIndex() {
		return currentImageIndex;
	}

	public void setCurrentImageIndex(int currentImageIndex) {
		this.currentImageIndex = currentImageIndex;
	}

	public void draw(Graphics g){
		g.drawImage(imageList.get(currentImageIndex), xPosition, yPosition, null);
	}
	
}
